/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
package com.wendy.common.core.utils.reflect;

import org.springframework.util.Assert;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

/**
 * 跟Field相关的工具类
 *
 * @author Mr.css
 * @version 2021-02-08 11:42
 */
public class FieldUtils {
    private FieldUtils() {
    }

    /**
     * 获取一个对象的所有非静态字段，包括父类的
     *
     * @param clazz -
     * @return field array
     */
    
    public static Field[] getFields(Class<?> clazz) {
        List<Field> ret = new ArrayList<>();
        Class<?> cla = clazz;
        while (cla != Object.class) {
            Field[] fields = cla.getDeclaredFields();
            for (Field field : fields) {
                if (!Modifier.isStatic(field.getModifiers())) {
                    ret.add(field);
                }
            }
            cla = cla.getSuperclass();
        }
        return ret.toArray(new Field[0]);
    }


    /**
     * 查找一个对象的字段，一直遍历到Object为止
     *
     * @param obj 对象
     * @param name  名称
     * @return 字段
     */
    
    public static Object getValue(Object obj, String name) throws IllegalAccessException {
        return getValue(obj, name, true);
    }

    /**
     * 查找一个对象的字段，一直遍历到Object为止
     *
     * @param obj 对象
     * @param name  名称
     * @return 字段
     */
    
    public static Object getValue(Object obj, String name, boolean accessible) throws IllegalAccessException {
        Field field = getField(obj.getClass(), name);
        Assert.notNull(field, "filed not found: " + name);
        field.setAccessible(accessible);
        return field.get(obj);
    }

    /**
     * 查找一个对象的字段，一直遍历到Object为止
     *
     * @param clazz 对象
     * @param name  名称
     * @return 字段
     */
    
    public static Field getField(Class<?> clazz, String name) {
        Class<?> cla = clazz;
        while (cla != Object.class) {
            try {
                return cla.getDeclaredField(name);
            } catch (NoSuchFieldException ignore) {
                cla = cla.getSuperclass();
            }
        }
        return null;
    }
}
